package servlet;

import dao.FactoryDao;
import modelo.Paciente;
import modelo.HistoriaClinica;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import static servlet.CapturadorDatosPaciente.historias;
 /**
 *
 * @author Miguel
 */
@WebServlet(name = "CapturadorDatosHC", urlPatterns = {"/CapturadorDatosHC"})
public class CapturadorDatosHC extends HttpServlet {
    private int CedHC;
    String Mensaje;
    public HistoriaClinica historia;
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try {
            CedHC=Integer.parseInt(request.getParameter("CedulaPaciente"));
            if(ExistePaciente(CedHC))
            {
                try
                {
                    historia=new HistoriaClinica(
                            Long.parseLong(request.getParameter("CedulaPaciente")),
                            request.getParameter("ListaEnfermedades"),
                            request.getParameter("ListaMedicamentos"),
                            Integer.parseInt(request.getParameter("PrecionArterial")),
                            Integer.parseInt(request.getParameter("Pulso")),
                            Integer.parseInt(request.getParameter("Temperatura")),
                            Integer.parseInt(request.getParameter("Respiracion")));
                    FactoryDao dao=FactoryDao.getFactoryDao(FactoryDao.POSTGRES_FACTORY);
                    Mensaje=dao.getusuarioDao().crearhistoriaclinica(historia);
                }
                catch(NumberFormatException e)
                {
                    Mensaje="Datos invalidos error "+e.getMessage();
                }
            }
            else
            {
                Mensaje=" El paciente con la cedula o nombre "+CedHC+" no existe";
            }
        }  
         catch (NumberFormatException e) 
         {
             Mensaje="Se presento un error en la captura de la cedula del paciente "+e.getMessage();
        }
        request.setAttribute("Mensaje",Mensaje);
        RequestDispatcher rd = request.getRequestDispatcher("./Registro_HC.jsp");
        rd.forward(request, response);
    }

    private boolean ExistePaciente(long CedHC)
    {
        try {
            FactoryDao dao=FactoryDao.getFactoryDao(FactoryDao.POSTGRES_FACTORY);
            return dao.getusuarioDao().validarpaciente(CedHC);
        } catch (Exception e) {
            Mensaje="Se presento un error en la validacion del usuario"+e.getMessage();
            return false;
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
